package com.webblog.controller;

import com.webblog.pojo.Classify;
import com.webblog.service.classify.ClassifyService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/classify")
public class ClassifyController {

    Logger logger = Logger.getLogger(ClassifyController.class);

    @Autowired
    private ClassifyService classifyService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map addClassify(Classify classify){
        Map resultMap = new HashMap();
        if(classifyService.addClassify(classify)>0){
            resultMap.put("success",true);
            resultMap.put("msg","添加分类成功");
        }
        else {
            resultMap.put("success",false);
            resultMap.put("msg","添加分类失败");
        }
        return resultMap;
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map removeClassify(@PathVariable int id){
        Map resultMap = new HashMap();
        if(classifyService.removeClassify(id)>0){
            resultMap.put("success",true);
            resultMap.put("msg","删除分类成功");
        }
        else {
            resultMap.put("success",false);
            resultMap.put("msg","删除分类失败");
        }
        return resultMap;
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Map updateClassify(Classify classify){
        Map resultMap = new HashMap();
        if(classifyService.updateClassify(classify)>0){
            resultMap.put("success",true);
            resultMap.put("msg","修改分类成功");
        }
        else {
            resultMap.put("success",false);
            resultMap.put("msg","修改分类失败");
        }
        return resultMap;
    }

    @RequestMapping(value = "/select",method = RequestMethod.GET)
    @ResponseBody
    public Map findAllClassify(){
        Map resultMap = new HashMap();
        List<Classify> classifyList = classifyService.findAllClassify();
        resultMap.put("data",classifyList);
        return resultMap;
    }


    @RequestMapping(value = "/select/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map findClassifyById(@PathVariable int id){
        Map resultMap = new HashMap();
        Classify classify = classifyService.findClassifyById(id);
        resultMap.put("data",classify);
        return resultMap;
    }


}
